track_pos=aux_data(9,:);
act_raw=ROIs(sel_roi).activity;
%act_raw=np.activity;

% curr_range=[1:20000];
% curr_range=[20000:60000];
% curr_range=[60000:80000];


track_pos_ft=track_pos(frame_times);

% act_raw=act_raw(curr_range);
% track_pos_ft=track_pos_ft(curr_range);

maxlim=max(track_pos);
minlim=min(track_pos);

last_turn=1;
turn_u=0;
turn_d=0;
refract=0;
dir_ind=zeros(length(track_pos),1);
last_turn_dir=NaN;
    
for ind=1:length(track_pos)

    if refract==1
        if abs(track_pos(ind))<1
            refract=0;
        end
    end

    if refract==0
        if track_pos(ind)<minlim+0.1
            turn_u=1;
            refract=1;
        elseif track_pos(ind)>maxlim-0.1
            turn_d=1;
            refract=1;
        end
    end

    if turn_u
        dir_ind(last_turn:ind)=1;
        turn_u=0;
        last_turn=ind;
        last_turn_dir=1;
    elseif turn_d
        turn_d=0;
        last_turn=ind;
        last_turn_dir=0;
    end
end

if last_turn_dir==0
    dir_ind(last_turn:end)=1;
end

dir_ind=logical(dir_ind);
dir_ind_ft=dir_ind(frame_times);

% dir_ind_ft=dir_ind_ft(curr_range);

%smooth raw activity trace
act=act_raw;
act=psmooth(act_raw);
act=act/median(act);

nbins=1000;
bins=[minlim:(maxlim-minlim)/nbins:maxlim];

act_n=[];

for knd=1:nbins
    act_n(knd,1)=mean(act(track_pos_ft>=bins(knd)&track_pos_ft<bins(knd+1)&dir_ind_ft'==1));
    act_n(knd,2)=mean(act(track_pos_ft>=bins(knd)&track_pos_ft<bins(knd+1)&dir_ind_ft'==0));
end



ccc=zeros(length(track_pos_ft),1);
ccc(track_pos_ft<0.5&dir_ind_ft'==1)=3;
ccc(track_pos_ft>=0.5&dir_ind_ft'==1)=2;
ccc(track_pos_ft<0.5&dir_ind_ft'==0)=0;
ccc(track_pos_ft>=0.5&dir_ind_ft'==0)=1;


%activity versus track pos
figure(1)
clf;
hold on

for ind=1:4
    area([1:length(ccc)],4*(ccc==ind-1),'facecolor',[1 1 1]*(1-(ind-1)/10),'linestyle','none')
end

plot((track_pos_ft/10)+0.5)
plot(act+0.5)
plot(obj_id_ft)
box off
%set(gcf,'position',fig_pos1);

% %track pos split into turn around points
% figure(2);
% clf;
% plot(track_pos)
% hold on
% plot(dir_ind,'r')
% xlabel('Time [s]')
% ylabel('Track position')
% set(gca,'xtick',[0:1e5:1e6],'xticklabel',[0:100:1000])
% set(gca,'ytick',[-5:2:5],'yticklabel',[0:20:100])
% ylim([-6 6])
% box off
% 
% %activity per track pos split into two directions (cut out track ends)
% figure(3)
% clf;
% hold on
% plot(track_pos_ft(~dir_ind_ft),act(~dir_ind_ft),'r.')
% plot(track_pos_ft(dir_ind_ft),act(dir_ind_ft),'.')
% xlim([-5.5 5.5])
% box off

% %binned activity
% figure(4)
% clf;
% plot(act_n)
% box off

%%activity per track pos rate map in both directions (averaged over entire
%%data set)
figure;imagesc([smooth(act_n(:,1),20) smooth(act_n(:,2),20)]')
colorbar
%caxis([0.965 1.315])
%caxis([0.98 1.2])
xlabel('Track position [1000 bins]')
box off
%set(gcf,'position',fig_pos2);

fig_pos1=get(gcf,'position');
fig_pos2=get(gcf,'position');
